using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using Web.Core;

namespace AsmxData
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ProductHandler : System.Web.Services.WebService
    {
        /// 
        [WebMethod]
        public string getList(string injson)
        {
		    JsonFormat jsf = new JsonFormat(injson);
		    if (!Authority.CheckAuthOnAsmx("ProductHandler.getList",jsf.GetValue("authid"), jsf.GetValue("authstr"), true))
			  return "{\"result\":\"-1\"}";
            int pagesize = Int16.Parse(jsf.GetValue("rows"));
            int currentpage = Int16.Parse(jsf.GetValue("page"));
            PageInfo pi = new PageInfo();
            pi.PageSize = pagesize;
            pi.PageIndex = currentpage;
    		Model.Product mdl = new Model.Product();
        
            PageList<Model.Product> lst = new BLL.Product().GetPageList(pi, mdl);
            string json = "{\"total\":" + pi.RecordCount.ToString()+",\"rows";
            json = Newtonsoft.Json.JavaScriptConvert.SerializeObject(lst).Replace("{\"List", json);
			return json;
        }

        /// 
        [WebMethod]
        public string getOne(string injson)
        {
		    JsonFormat jsf = new JsonFormat(injson);
		    if (!Authority.CheckAuthOnAsmx("ProductHandler.getOne",jsf.GetValue("authid"), jsf.GetValue("authstr"), true))
			  return "{\"result\":\"-1\"}";
            int? id = BLL.BLHelper.GetInt(jsf.GetValue("id"));
	    	Model.Product wheremdl = new Model.Product();
    		wheremdl.ObjectId = id;
            Model.Product mdl = new BLL.Product().GetModel(wheremdl);
            string json = "\"result\":{0},\"model\":{1}";
            if (mdl == null)
                json = String.Format(json, 0, "{}");
            else
                json = String.Format(json, 1, Newtonsoft.Json.JavaScriptConvert.SerializeObject(mdl));
            return "{" + json + "}";
        }

        /// 
        [WebMethod]
        public string AddOne(string injson)
        {
		    JsonFormat jsf = new JsonFormat(injson);
		    if (!Authority.CheckAuthOnAsmx("ProductHandler.AddOne",jsf.GetValue("authid"), jsf.GetValue("authstr"), true))
			  return "{\"result\":\"-1\"}";
            Model.Product mdl = new Model.Product();
	
            mdl.SellType = BLL.BLHelper.GetString(jsf.GetValue("SellType"));
            mdl.BrandType = BLL.BLHelper.GetString(jsf.GetValue("BrandType"));
            mdl.StyleType = BLL.BLHelper.GetString(jsf.GetValue("StyleType"));
            mdl.Icon = BLL.BLHelper.GetString(jsf.GetValue("Icon"));
            mdl.Name = BLL.BLHelper.GetString(jsf.GetValue("Name"));
            mdl.Info = BLL.BLHelper.GetString(jsf.GetValue("Info"));
            mdl.PropertyType = BLL.BLHelper.GetString(jsf.GetValue("PropertyType"));
            mdl.Property = BLL.BLHelper.GetString(jsf.GetValue("Property"));
            mdl.Price = BLL.BLHelper.GetDecimal(jsf.GetValue("Price"));
            mdl.VipPrice = BLL.BLHelper.GetDecimal(jsf.GetValue("VipPrice"));
            mdl.UnitName = BLL.BLHelper.GetString(jsf.GetValue("UnitName"));
            mdl.StoreNum = BLL.BLHelper.GetInt(jsf.GetValue("StoreNum"));
            mdl.UpTime = BLL.BLHelper.GetDateTime(jsf.GetValue("UpTime"));
            mdl.IsNew = BLL.BLHelper.GetBool(jsf.GetValue("IsNew"));
            mdl.IsHot = BLL.BLHelper.GetBool(jsf.GetValue("IsHot"));
            mdl.IsHome = BLL.BLHelper.GetBool(jsf.GetValue("IsHome"));
            mdl.IsTypeHome = BLL.BLHelper.GetBool(jsf.GetValue("IsTypeHome"));
            new BLL.Product().Add(mdl);
            return "{\"result\":\"1\"}";
        }

        /// 
        [WebMethod]
        public string RemoveOne(string injson)
        {
		    JsonFormat jsf = new JsonFormat(injson);
		    if (!Authority.CheckAuthOnAsmx("ProductHandler.RemoveOne",jsf.GetValue("authid"), jsf.GetValue("authstr"), true))
			  return "{\"result\":\"-1\"}";
            int? id = BLL.BLHelper.GetInt(jsf.GetValue("id"));
            Model.Product wheremdl = new Model.Product();
	    	wheremdl.ObjectId = id;
	    	new BLL.Product().Delete(wheremdl);
	    	return "{\"result\":\"1\"}";
        }

        /// 
        [WebMethod]
        public string UpdataOne(string injson)
        {
		    JsonFormat jsf = new JsonFormat(injson);
		    if (!Authority.CheckAuthOnAsmx("ProductHandler.UpdataOne",jsf.GetValue("authid"), jsf.GetValue("authstr"), true))
			  return "{\"result\":\"-1\"}";
            Model.Product setmdl = new Model.Product();
	    	Model.Product wheremdl = new Model.Product();
	
            setmdl.ObjectId = BLL.BLHelper.GetInt(jsf.GetValue("ObjectId"));
            setmdl.SellType = BLL.BLHelper.GetString(jsf.GetValue("SellType"));
            setmdl.BrandType = BLL.BLHelper.GetString(jsf.GetValue("BrandType"));
            setmdl.StyleType = BLL.BLHelper.GetString(jsf.GetValue("StyleType"));
            setmdl.Icon = BLL.BLHelper.GetString(jsf.GetValue("Icon"));
            setmdl.Name = BLL.BLHelper.GetString(jsf.GetValue("Name"));
            setmdl.Info = BLL.BLHelper.GetString(jsf.GetValue("Info"));
            setmdl.PropertyType = BLL.BLHelper.GetString(jsf.GetValue("PropertyType"));
            setmdl.Property = BLL.BLHelper.GetString(jsf.GetValue("Property"));
            setmdl.Price = BLL.BLHelper.GetDecimal(jsf.GetValue("Price"));
            setmdl.VipPrice = BLL.BLHelper.GetDecimal(jsf.GetValue("VipPrice"));
            setmdl.UnitName = BLL.BLHelper.GetString(jsf.GetValue("UnitName"));
            setmdl.StoreNum = BLL.BLHelper.GetInt(jsf.GetValue("StoreNum"));
            setmdl.UpTime = BLL.BLHelper.GetDateTime(jsf.GetValue("UpTime"));
            setmdl.IsNew = BLL.BLHelper.GetBool(jsf.GetValue("IsNew"));
            setmdl.IsHot = BLL.BLHelper.GetBool(jsf.GetValue("IsHot"));
            setmdl.IsHome = BLL.BLHelper.GetBool(jsf.GetValue("IsHome"));
            setmdl.IsTypeHome = BLL.BLHelper.GetBool(jsf.GetValue("IsTypeHome"));

            new BLL.Product().Update(setmdl, wheremdl);
            return "{\"result\":\"1\"}";
        }
    }
}
  